package com.dbq.share;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.PopupWindow;

import com.tencent.mm.opensdk.modelbase.BaseResp;

import java.util.UUID;

/**
 * Created by sunkai on 2017/12/26.
 * <p>
 * 分享工具类
 * <p>
 * 提供 微信、朋友圈、QQ、QQ空间、微博、Facebook 、复制链接分享功能
 * <p>
 * 其中 微信、朋友圈需要提供  WECHAT_APPID
 * QQ、QQ空间需要提供   QQ_APPID      （只要提供 appId，分享功能不需要在 qq 开放平台创建 app）
 * 微博需要提供         SINA_APPKEY   （客户端分享只需要提供 appkey，如果没有客户端，h5 分享需要在微博开放平台授权通过，否则只能分析测试用户）
 */

public final class ShareUtil {
    public static final String QQ_APPID = "1106636348";
    public static final String WECHAT_APPID = "wxe60d1b76fdcf5504";
    public static final String SINA_APPKEY = "3980692944";
    public static final String SIAN_REDIRECT_URL = "https://www.jianshu.com/";
    public static final String SINA_SCOPE = "all";

    private SharePlatformSelector sharePlatformSelector;
    private ShareWebPage shareWebPage;
    private String uuid;
    private boolean startShare;

    private ShareUtil() {

    }

    public static ShareUtil instance() {
        return new ShareUtil();
    }


    private AdapterView.OnItemClickListener mShareItemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ShareTarget item = (ShareTarget) parent.getItemAtPosition(position);
            shareTo(view.getContext(), item);
            startShare = true;
            hideShareWindow();
        }
    };

    private void shareTo(Context context, ShareTarget item) {
        YibaShare.getShareClient().share(context, item, shareWebPage, uuid);
    }

    private PopupWindow.OnDismissListener onDismissListener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            if (!startShare) {
                ShareCallback callback;
                if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
                    callback.shareFail(SocializeMediaConstant.NONE, ShareCallback.Code.USER_CANCEL_DIALOG, "user cancel selector dialog");
                    YibaShare.getShareClient().shareCallback.remove(uuid);
                }
                sharePlatformSelector.cancleWindowAlpha();
            }
        }
    };

    private void hideShareWindow() {
        if (sharePlatformSelector != null) {
            sharePlatformSelector.dismiss();
        }
    }

    public void showShareDialog(View anchor, Activity activity,ShareWebPage shareWebPage, ShareCallback shareCallback) {
        sharePlatformSelector = new SharePlatformSelector(activity,anchor, mShareItemClick, onDismissListener);
        this.shareWebPage = shareWebPage;
        this.uuid = UUID.randomUUID().toString();
        sharePlatformSelector.show();
        shareCallback.showSelectorDialog();
        YibaShare.getShareClient().shareCallback.put(uuid, shareCallback);
    }

    public static void handlerWxShareOnRespData(Context context, BaseResp baseResp) {
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if (baseResp.getType() == 2) {
                    //分享成功
                    Intent intent = new Intent();
                    intent.setAction(WxAssistActivity.KEY_BROADCAST_ACTION);
                    intent.putExtra(WxAssistActivity.KEY_BROADCAST_SHARE_SUCCESS, true);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                if (baseResp.getType() == 2) {
                    //分享取消
                    Intent intent = new Intent();
                    intent.setAction(WxAssistActivity.KEY_BROADCAST_ACTION);
                    intent.putExtra(WxAssistActivity.KEY_BROADCAST_SHARE_SUCCESS, false);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
                }
                break;
        }
    }
}
